new Vue({
    el: '#mainBody',
    data: function () {
        return {
            dialogVisible: false, //新建数据dialog
            showDialog: false, //修改dialog
            viewTitle: '', //修改title
            isDisabled: false, //修改dialog disabled
            form: [], //修改的数据
            searchForm: {
                select: '',
                person: ''
            },
            dataKeys: [ //名称
            ],
            currentpage:1,//当前页数
            pageBean:[//分页参数列表
            ],
            dataList: [ //数据列表
            ],
            ruleForm: { //form v-model
                selectDataKey: '',
                dataValue: ''
            },
            rules: { //新建from 验证
                selectDataKey: [
                    {required: true, message: '请选择名称', trigger: 'change'}
                ]
            },
            changeRules: { //修改from 验证
                dataValue: [
                    {required: true, message: '值不能为空', trigger: 'blur'}
                ]
            },
            searchForm2 : { // 查询条件
            	select: '',
                person: ''
			},
			start : false,
			start2 : false,
			start3 : false,
			dynamicTags: [],
	        inputValue: ''
        };
    },
    created: function () {
    	this.getAllDataDictionaryList()
    	this.selectKeys()
    },
    methods: {
    	/**
    	 * 下一页上一页按钮
    	 * */
    	handleCurrentChange:function(val){
    		this.currentpage=val;
    		if (this.start) {
				var data = "";
	            if(this.searchForm.select||this.searchForm.person){//两者只要有一者不为空
	            	data = JSON.stringify(this.searchForm);
	            }
			} else {
				var data = "";
			}
    		this.getAllDataDictionaryList(data);
    	},
        /**
         * 获取数据字典列表
         */
        getAllDataDictionaryList:function (data) {
            var _this = this;
            if (!data) {//如果data是空的
				if (this.start) {
					var data = "";
		            if(this.searchForm.select||this.searchForm.person){//两者只要有一者不为空
		            	data = JSON.stringify(this.searchForm);
		            }
				} else {
					var data = "";
				}
			}
            $.ajax({
                url: '/findAllDataDictionary',
                type: 'post',
                data:{"data":data,"page":_this.currentpage},
                dataType: 'json',
                success: function (res) {
                	_this.exhibition(res);
                	sessionStorage.setItem('length',res.data.beanList.length);
                },
                error: function (err) {
                	if(err.responseJSON.message=="session clean"){
                        errorMessage(_this.$message, '登陆已失效重新登陆!');
                        toLogin();
                        return;
                    }else if (err.status == 403){
                        errorMessage(_this.$message, "没有权限，请先申请该权限！");
                        return;
                    }
                	errorMessage(_this.$message, '获取数据字典列表出错！')
                }
            })
        },
        /**
         * 多条件查询
         * */
        selectKeysAndUser:function () {
        	this.currentpage = 1;
        	if(this.searchForm.select=="" && this.searchForm.person==""){
	        	if (this.start2) {
					this.start = false;
					this.start2 = false;
	        	}
			} else {
				this.start = true;
			}
        	this.getAllDataDictionaryList();
        },
        /**
         * 
         * 处理页面数据展示
         * */
        exhibition: function(res){
        	var _this = this;
        	var datadic_arr=[];
        	res.data.beanList.forEach(function (obj) {
                var datadic = {}
                datadic['id']=obj.id
                datadic['dataKeys']=obj.dataKeys
                datadic['dataValue']=obj.dataValue
                datadic['uploadId']=obj.updateUser
                datadic['uploadDate']=obj.updateTimes;
                datadic_arr.push(datadic)
            })
            this.dataList = datadic_arr
            var page = {}
            page['totalNumber'] = res.data.totalNumber
            page['totalPage'] = res.data.totalPage
            this.pageBean = page
        },
        createDatas: function (formName) { //新建数据
        	//打开新页面
//	    	if(this.ruleForm.selectDataKey || this.dynamicTags!=0){
//	        	this.ruleForm.selectDataKey=""
//	        	this.dynamicTags = []
//	    	}
            this.dialogVisible = true
            this.form = [];
        },
        saveForm: function (formName) { //保存
        	var _this = this;
            this.$refs[formName].validate(function (valid) {
                if (valid && _this.start3) {
                	_this.ruleForm.dataValue = _this.dynamicTags
                    _this.dialogVisible = false
                	var dataKeys = _this.ruleForm.selectDataKey//获取名称文本
                	var dataValue = _this.ruleForm.dataValue//获取值文本
                    var data = "";
                    if(_this.ruleForm.selectDataKey || _this.ruleForm.dataValue){//两者只要有一者不为空
                    	data = JSON.stringify(_this.ruleForm);
                    }
                	$.ajax({
                    	url: '/addDataDictionary',
                    	data:{"data":data},
                        type: 'post',
                        dataType: 'json',
                        success: function (res) {
                        	if(res.data){
                        		successMessage(_this.$message, '新增数据字典成功！')
                        		$.ajax({
					            	url: '/refreshDataDictionarys',
					                type: 'get',
					                async: false
					            })
                        	}
                        	else{
                        		errorMessage(_this.$message, '新增数据字典出错！')
                        	}
                        	_this.getAllDataDictionaryList()
                        },
                        error: function (err) {
                        	if(err.responseJSON.message=="session clean"){
                                errorMessage(_this.$message, '登陆已失效重新登陆!');
                                toLogin();
                                return;
                            }else if (err.status == 403){
                                errorMessage(_this.$message, "没有权限，请先申请该权限！");
                                return;
                            }
                        	errorMessage(_this.$message, '新增数据字典出错！')
                        }
                    })
                } else {
                    return false
                }
            })
        },
        selectKeys: function (){
        	var _this = this;
            //加载dataKeys
            $.ajax({
                url: '/getkeyDataDictionary',
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    var orgns_arr=[];
                	res.data.forEach(function (obj) {
                        var orgns = {}
                        orgns['value']=obj.value
                        orgns['label']=obj.label
                        orgns_arr.push(orgns)
                    })
                    _this.dataKeys = orgns_arr
                },
                error: function (err) {
                	if(err.responseJSON.message=="session clean"){
                        errorMessage(_this.$message, '登陆已失效重新登陆!');
                        toLogin();
                        return;
                    }else if (err.status == 403){
                        errorMessage(_this.$message, "没有权限，请先申请该权限！");
                        return;
                    }
                	errorMessage(_this.$message, '获取数据字典keys列表出错！')
                }
            })
        },
        resetForm: function (formName) { //清空
            this.$refs[formName].resetFields();
            this.start = false;
			this.start2 = true;
			this.currentpage = 1;
			this.getAllDataDictionaryList();
        },
        changeItem: function (index) { //修改
            this.showDialog = true
            this.selectedForm = index
            this.form = JSON.parse(JSON.stringify(this.dataList[index]))
            this.viewTitle = '修改数据值'
            this.isDisabled = false
        },
        saveChangeForm: function (formName) { //保存修改
            var _this = this
            this.$refs[formName].validate(function (valid) {
                if (valid) {
                    _this.showDialog = false
                    var id = _this.form.id
                    var dataValue = _this.form.dataValue//获取值文本
                	$.ajax({
                    	url: '/updateDataDictionaryById/'+id,
                    	data:{"dataValue":dataValue},
                        type: 'post',
                        dataType: 'json',
                        success: function (res) {
                            Vue.set(_this.dataList, _this.selectedForm, _this.form)
                            successMessage(_this.$message, '修改数据字典成功！')
                            $.ajax({
				            	url: '/refreshDataDictionarys',
				                type: 'get',
				                async: false
				            })
                            _this.getAllDataDictionaryList()
                        },
                        error: function (err) {
                        	if(err.responseJSON.message=="session clean"){
                                errorMessage(_this.$message, '登陆已失效重新登陆!');
                                toLogin();
                                return;
                            }else if (err.status == 403){
                                errorMessage(_this.$message, "没有权限，请先申请该权限！");
                                return;
                            }
                        	errorMessage(_this.$message, '修改数据字典出错！')
                        }
                    })
                } else {
                    return false
                }
            })
        },
        closeDialog: function (formName) { //关闭清除校验结果
            this.$refs[formName].clearValidate()
	        this.dynamicTags = []
            this.ruleForm.selectDataKey = "";
            $(".input-new-tag").parent('div').css('border','1px solid #dcdfe6')
	    	$("#span1").html("");
        },
        deleteItem: function (index) { //删除
        	var _this = this
        	var length = sessionStorage.getItem('length');
        	if(length==1){
        		this.currentpage = this.currentpage-1;
        	}
        	var id = JSON.parse(JSON.stringify(this.dataList[index])).id;

            this.$confirm('确认要删除?删除后不可恢复', '删除提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                $.ajax({
                    url: '/deleteDataDictionaryById/'+id,
                    type: 'post',
                    dataType: 'json',
                    success: function (res) {
                        successMessage(_this.$message, '删除数据字典数据成功！')
                        $.ajax({
                            url: '/refreshDataDictionarys',
                            type: 'get',
                            async: false
                        })
                        _this.getAllDataDictionaryList()
                    },
                    error: function (err) {
                        if(err.responseJSON.message=="session clean"){
                            errorMessage(_this.$message, '登陆已失效重新登陆!');
                            toLogin();
                            return;
                        }else if (err.status == 403){
                            errorMessage(_this.$message, "没有权限，请先申请该权限！");
                            return;
                        }
                        errorMessage(_this.$message, '删除数据字典数据出错！')
                    }
                })
            }).catch(function () {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

	        },
	        handleClose: function (tag) {//删除值
	            this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
	            this.lose();
	          },
	      handleInputConfirm: function () {//保存要添加的值
	    	var _this = this
	        var inputValue = this.inputValue;
	        if (inputValue) {
	        	var bool = this.remaveRepeat(inputValue);
	        	if(bool){
	        		this.dynamicTags.push(inputValue);
	        	}
	        }
	        this.inputValue = '';
	        this.lose();
	      },
	      remaveRepeat: function(inputValue){
	    	  var count = 0;
	    	  if(this.dynamicTags.length!=0){
	    		  for(var i=0; i<this.dynamicTags.length; i++){
	    			  if(this.dynamicTags[i] == inputValue){
	    				  count++;
	    			  }
	    		  }
	    		  if(count == 0){
	    			  return true;
	    		  }else{
	    			  return false;
	    		  }
	    	  }else{
	    		  return true;
	    	  }
	      },
	      lose: function(){
	    	  if(this.dynamicTags.length==0 && this.inputValue==''){
	      		$("#span1").html("请输入值").css("color","#f56c6c").css("font-size","12px");
	      		$(".input-new-tag").parent('div').css('border','1px solid #f56c6c')
	      		this.start3 = false;
	      	  }else{
	      		$("#span1").html("");
	      		$(".input-new-tag").parent('div').css('border','1px solid #67c23a')
	      		this.start3 = true;
	      	  }
	      }
	       
    }
})
$(function () {
    $('.pcoded-left-item').children('li').siblings().removeClass('active pcoded-trigger')
    $('#5').addClass('active pcoded-trigger')
    $('#53').addClass('active')
})